
package org.ejc.util;

import javax.jms.JMSException;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.ejc.email.EmailMessage;


/**
 * @author zac
 * 
 * 
 */
public class JMSUtil {

    
    public static void sendToEmailQ(String queueName,EmailMessage ejcMessage)throws NamingException,JMSException{
        sendToJMSQueue( queueName, ejcMessage);
    }
    
    
    public static void sendToJMSQueue(String queueName, EmailMessage obj) throws NamingException, JMSException {

        InitialContext ic = null;
        QueueConnection cnn = null;
        QueueSender sender = null;
        QueueSession session = null;

        try {
            ic = new InitialContext();

            Queue queue = (Queue) ic.lookup(queueName);
            QueueConnectionFactory factory = (QueueConnectionFactory) ic.lookup("java:/JmsXA");

            cnn = factory.createQueueConnection();
            session = cnn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
            
            sender = session.createSender(queue);
            ObjectMessage message=session.createObjectMessage(obj);
           
            
            
            sender.send(message);
        } finally {
            if (sender != null) {
                sender.close();
            }

            if (session != null) {
                session.close();
            }

            if (cnn != null) {
                cnn.close();
            }

            if (ic != null) {
                ic.close();
            }
        }
    }

}